package com.lj.api.v1.a;


import com.lj.api.auth.JwtProperties;
import com.lj.api.auth.JwtTools;
import com.lj.api.common.CodeMsg;
import com.lj.api.common.Md5Encrypt;
import com.lj.api.common.Result;
import com.lj.api.common.WebUtil;
import com.lj.api.tools.AccessoryTools;
import com.lj.mcore.entity.Store;
import com.lj.mcore.entity.User;
import com.lj.mcore.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Tag(name = "用户管理", description = "用于管理用户信息")
@RestController
@RequestMapping("/a")
public class IndexController {

    @Autowired
    IUserService userService;

    @Operation(summary = "用户登录", description = "根据用户ID查询用户的详细信息")
    @ApiResponse(responseCode = "200", description = "成功获取用户信息")
    @ApiResponse(responseCode = "404", description = "未找到对应用户")
    @RequestMapping("/login")
    public String login( @RequestParam("name")String name,@RequestParam("password")String password,@RequestParam("checkCode")String checkCode){

        if (WebUtil.checkEmpty(name,password,checkCode)){
            return Result.error(CodeMsg.PARAMETER_ISNULL);
        }

        LinkedHashMap<String,Object> params = new LinkedHashMap<>();
        params.put("userName",name);
        params.put("password", Md5Encrypt.md5(password).toLowerCase());

        User user = userService.getSingleResult(params);
        Map map = new HashMap();
        if (user==null){
            return Result.fail(CodeMsg.LOGIN_FAILD);
        }

        map.put("uid",user.getId());
        map.put("userName",user.getUserName());
        map.put("trueName",user.getTrueName());

        if (user.getPhoto()!=null){
            map.put("photo", AccessoryTools.getProcessURL_QN(user.getPhoto(),1));
        }else{
            map.put("photo","");
        }
        Store store = user.getStore();
        if (store!=null){
            map.put("sid",store.getId());
        }
        map.put("roles",user.getUserRole());
        String token = JwtTools.generateToken(user.getId()+"", JwtProperties.WX_AUD,new Date().getTime(),JwtProperties.WEB_VALID_TIME);
        map.put("access_token",token);

        return Result.ok(map);
    }

}
